(****************************************************)
(*													*)
(*	file:  Distributions.m							*)
(*													*)
(*	See definition module for documentation of 		*)
(*	functions.			 							*)
(*													*)
(*	Written in SemperSoft Modula-2 v.1.1.2			*)
(*													*)
(*	Allen Stenger	May 1989  						*)
(*													*)
(****************************************************)
IMPLEMENTATION MODULE Distributions;

FROM MathLib0		IMPORT	ln;
FROM InsideMac		IMPORT	Random;

PROCEDURE UniformZeroToOne() : REAL;
BEGIN
	RETURN( FLOAT(Random()) / 65534.0 + 0.5 )
END UniformZeroToOne;

PROCEDURE UniformDistribution( 
			start, end : CARDINAL ) : CARDINAL;
BEGIN
	RETURN( start 
			+ TRUNC((FLOAT(end - start) + 1.0) 
				* UniformZeroToOne()) );
END UniformDistribution;

PROCEDURE ExponentialDistribution( 
			mean : CARDINAL ) : CARDINAL;
CONST
	maxCard	=	FLOAT(MAX(CARDINAL));
VAR
	r	:	REAL;
BEGIN
	r := - FLOAT(mean) * ln( UniformZeroToOne() );
	IF r > maxCard THEN r := maxCard END;
	RETURN( TRUNC(r) ); 
END ExponentialDistribution;

END Distributions.
